<?php


class Ferramenta implements IObject {

    const ANOTACAO = 1;
    const AVISO = 2;
    const CHAMADO_PREFERIDO = 3;
    const FAQ = 4;
    const CALLED_WAITING_FOR_ME = 5;
    const BIRTHDAYS = 6;

    public $tMysqli;
    private $IDFerramenta;
    private $IDUsuario;
    private $StFerramenta;
    private $StDescricao;
    private $StIconName;
    private $ItMax;

    private $Usuario;
    private $ArList;

    public function __construct( $ArDadosFerramenta, &$tMysqli ) {
        $this->IDFerramenta = !empty($ArDadosFerramenta['IDFerramenta']) ? $ArDadosFerramenta['IDFerramenta']: false;
        $this->IDUsuario = !empty($ArDadosFerramenta['IDUsuario']) ? $ArDadosFerramenta['IDUsuario']: false;
        $this->StFerramenta = !empty($ArDadosFerramenta['StFerramenta']) ? $ArDadosFerramenta['StFerramenta']: false;
        $this->StDescricao = !empty($ArDadosFerramenta['StDescricao']) ? $ArDadosFerramenta['StDescricao']: false;
        $this->StIconName = !empty($ArDadosFerramenta['StIconName']) ? $ArDadosFerramenta['StIconName']: false;
        $this->ArList = array();
        $this->tMysqli = $tMysqli;
    }

    public function loadRank($ItMax) {
        $this->ItMax = $ItMax;
        switch($this->IDFerramenta) {
            case self::ANOTACAO:
                $this->loadAnnotationRank();
                break;
            case self::AVISO:
                $this->loadNoticeRank();
                break;
            case self::CHAMADO_PREFERIDO:
                $this->loadPreferredCalledRank();
                break;
            case self::FAQ:
                $this->loadFaqRank();
                break;
            case self::CALLED_WAITING_FOR_ME:
                $this->loadCalledWaitingForMeRank();
                break;
            case self::BIRTHDAYS:
                $this->loadBirthdaysRank();
                break;
        }
    }

    private function loadCalledWaitingForMeRank() {
        $StSql = TQuery::getSqlFindCalledWaitingFor($this->IDUsuario, $this->ItMax);
        $tQuery = $this->tMysqli->exec($StSql);
        while($ArCalledData = mysqli_fetch_assoc($tQuery)) {
            $this->ArList[] = Chamado::calledFactory($ArCalledData, $this->tMysqli);
        }
    }

    private function loadBirthdaysRank() {
        $StSql = TQuery::getSqlFindBirthdaysIn(TIME_NEXT_BIRTHDAYS);
        $tQuery = $this->tMysqli->exec($StSql);
        while($ArUsuarioData = mysqli_fetch_assoc($tQuery)) {
            if( $ArUsuarioData['IDUsuario'] == $this->IDUsuario ) {
                $this->ArList[] = $this->Usuario;
                continue;
            }
            $this->ArList[] = new Usuario($ArUsuarioData['IDUsuario'], $this->tMysqli);
        }
    }

    private function loadFaqRank() {
        $StSql = TQuery::getSqlFindAllQuestions($this->ItMax);
        $tQuery = $this->tMysqli->exec($StSql);
        while($ArPerguntaData = mysqli_fetch_assoc($tQuery)) {
            $this->ArList[] = new Pergunta($ArPerguntaData);
        }
    }

    private function loadPreferredCalledRank() {
        if( !$this->Usuario->hasPreferredCalled() ) { return; }
        foreach($this->Usuario->getArChamadosPreferidos() as $Chamado) {
            $this->ArList[] = $Chamado;
            if( $this->ItMax == count($this->ItMax) ) {
                return;
            }
        };
    }

    private function loadNoticeRank() {
        $StSql = TQuery::getSqlFindNoticesByUser($this->Usuario, $this->ItMax);
        $tQuery = $this->tMysqli->exec($StSql);
        while($ArNoticeData = mysqli_fetch_assoc($tQuery)) {
            $this->ArList[] = new Aviso($ArNoticeData);
        }
    }

    private function loadAnnotationRank() {
        $StSql = TQuery::getSqlFindNotesByUser($this->IDUsuario, $this->ItMax);
        $tQuery = $this->tMysqli->exec($StSql);
        while ($ArNotesData = mysqli_fetch_assoc($tQuery)) {
            $this->ArList[] = new Anotacao($ArNotesData);
        }
    }

    public function getIDFerramenta() {
        return $this->IDFerramenta;
    }

    public function setIDFerramenta($IDFerramenta) {
        $this->IDFerramenta = $IDFerramenta;
    }

    public function getIDUsuario() {
        return $this->IDUsuario;
    }

    public function setIDUsuario($IDUsuario) {
        $this->IDUsuario = $IDUsuario;
    }

    public function getStTable() {
        return $this->StTable;
    }

    public function setStTable($StTable) {
        $this->StTable = $StTable;
    }

    public function getStFerramenta() {
        return $this->StFerramenta;
    }

    public function setStFerramenta($StFerramenta) {
        $this->StFerramenta = $StFerramenta;
    }

    public function getStDescricao() {
        return $this->StDescricao;
    }

    public function setStDescricao($StDescricao) {
        $this->StDescricao = $StDescricao;
    }

    public function getStIconName() {
        return $this->StIconName;
    }

    public function setStIconName($StIconName) {
        $this->StIconName = $StIconName;
    }

    public function getFerramenta() {
        return $this->Ferramenta;
    }

    public function setFerramenta($Ferramenta) {
        $this->Ferramenta = $Ferramenta;
    }

    public function setUsuario($Usuario) {
        $this->Usuario = $Usuario;
    }

    public function getList() {
        return $this->ArList;
    }

    public function equals(IObject $Ferramenta) {
        return ($this->IDFerramenta == $Ferramenta->getIDFerramenta());



    }

}

?>
